﻿using MySql.Data.MySqlClient;
using PureMVC.Interfaces;
using SuperMap.Data;
using SuperMap.Sample.Data;
using System;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;

namespace Rail.Start.Viewer
{
    class ProjectFoundFormMediator : PureMVC.Patterns.Mediator, IMediator
    {
        ProjectFoundForm m_ProjectFoundForm;
        public ProjectFoundFormMediator(ProjectFoundForm viewComponent)
               : base(NAME, viewComponent)
        {
            m_ProjectFoundForm = viewComponent;
            m_ProjectFoundForm.SubmitButtonProjectFound.Click += OnClick;
        }

        private void OnClick(object sender, EventArgs e)
        {
            string project_engineering = "sys_project_engineering";
            string pro_name = m_ProjectFoundForm.ComboBoxProjectid.Text;
            if (!m_ProjectFoundForm.m_Pairs.ContainsKey(pro_name))
                return;
            string sql1 = Commons.MySqlHelper.GetAllField(project_engineering);
            DataTable dt_eng = Commons.MySqlHelper.GetDataTable(sql1, project_engineering);

            object eng_id;
            m_ProjectFoundForm.m_Pairs.TryGetValue(pro_name, out eng_id);
            //string InsertSql = "insert into " + project_engineering + " values ('" + eng_id + "','" + pro_name + "')";
            object[] values = new object[2];
            values[0] = eng_id;
            values[1] = pro_name;
            string InsertSql = Commons.MySqlHelper.WriteTableRow(project_engineering, values);
            Commons.MySqlHelper.GetDataTable(InsertSql, project_engineering);

            EstablishWorkspace(eng_id.ToString());
        }
        private WorkspaceConnectionInfo m_connectionInfo = new WorkspaceConnectionInfo();
        /// <summary>
        /// 创建SDB工作空间
        /// Create the SDB workspace
        /// </summary>
        /// <param name="workspace_name"> 工作空间文件名 --项目ID </param>
        private void EstablishWorkspace( string workspace_name)
        {
            try
            {
                //创建工作空间 SMWU 文件至本地路径
                #region
                m_connectionInfo.Type = WorkspaceType.SMWU;
                string m_path = System.IO.Path.Combine(Commons.Paths.ApplicationWorkspaceDir, workspace_name + "." + "SMWU");
                m_connectionInfo.Server = m_path;
                m_connectionInfo.Name = workspace_name;
                m_connectionInfo.Password = "";

                Workspace m_workspace = new SuperMap.Data.Workspace();
                //SampleRun m_sampleRun = new SampleRun(m_workspace);
                FormMain.formMain.m_sampleRun.Create(m_connectionInfo);
                #endregion

                DatasourceConnectionInfo m_datasourceInfo = CreateDatasource( workspace_name);  //创建数据源
                CopyDatasets( workspace_name);  //拷贝数据源

                SceneExtend m_scenecontrol = FormMain.formMain.m_sampleRun.CreateScene("NewScene"+workspace_name);    //新建场景
                SceneExtendMediator sem = new SceneExtendMediator(m_scenecontrol);
                

                FormMain.formMain.m_sampleRun.Save();  //保存新建工作空间
                #region
                FormMain.formMain.m_sampleRun.Open(m_connectionInfo);   //打开新建工作空间
                FormMain.formMain.m_sampleRun.OpenDatasource(m_datasourceInfo); //打开对应工作空间数据源
                #endregion
                m_ProjectFoundForm.SetComboBoxProjectid();  //初始化项目选择下拉列表 items
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
        /// <summary>
        /// 拷贝数据集
        /// </summary>
        /// <param name="m_workspace"></param>
        /// <param name="m_projectId"></param>
        private void CopyDatasets( string m_projectId)
        {
            Workspace mb_workspace = new Workspace();
            mb_workspace.Open(new WorkspaceConnectionInfo(System.IO.Path.Combine(Commons.Paths.ApplicationWorkspaceDir, "workspace.smwu")));
            Datasource mb_datasource = mb_workspace.Datasources[0];
            Datasource m_datasourceNew = FormMain.formMain.m_sampleRun.m_workspace.Datasources[0];
            Datasets mb_datasetsToCopy = mb_workspace.Datasources[mb_datasource.ConnectionInfo.Alias].Datasets;
            foreach (Dataset dataset in mb_datasetsToCopy)
            {
                m_datasourceNew.CopyDataset(dataset, dataset.Name , dataset.EncodeType);

            }
        }
        /// <summary>
        /// 给新建工作空间创建数据源
        /// </summary>
        /// <param name="m_sampleRun"></param>
        /// <param name="m_datasourceName"></param>
        private DatasourceConnectionInfo CreateDatasource(/*SampleRun m_sampleRun,*/ string m_datasourceName)
        {
            DatasourceConnectionInfo m_datasourceInfo = new DatasourceConnectionInfo();
            m_datasourceInfo.Alias = "Datasource_" + m_datasourceName;
            m_datasourceInfo.Server = Commons.Paths.ApplicationWorkspaceDir + "\\" + "Datasource_" + m_datasourceName;
            FormMain.formMain.m_sampleRun.CreateDatasource(m_datasourceInfo);
            return m_datasourceInfo;
        }
        

    }
}
